package com.zhongke.cotrun.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhongke.cotrun.service.PayMallInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongke.cotrun.mapper.PayMallInfoMapper;
import com.zhongke.cotrun.model.pojo.PayMallInfo;

/**
 * @Description: 商品基本信息
 * @Author: songjing
 * @Date:   2020-03-20
 * @Version: V1.0
 */
@Service
public class PayMallInfoServiceImpl extends ServiceImpl<PayMallInfoMapper, PayMallInfo> implements PayMallInfoService {
	@Autowired
	PayMallInfoMapper payMallInfoMapper;
	 /**
     * 新增
     * @param  对象
     * @return
     */
    public boolean cotrunInsert(PayMallInfo payMallInfo){
    	int r = payMallInfoMapper.cotrunInsert(payMallInfo);
    	if(r == 1)
    		return true;
    	else
    		return false;
    }
    /**
     * 根据主体主键查询商品主体信息
     *
     * @param 主体主键
     * @param 主体表名
     * @return
     */
	@Override
	public List<Map> selectMainDetailByMainId(Long mainId, String mainTable) {
		// TODO Auto-generated method stub
		return payMallInfoMapper.selectMainDetailByMainId(mainId, mainTable);
	}
	/**
     * 根据单号查询明细表列表
     *
     * @param 订单号
     * @param 明细表名
     * @return
     */
	@Override
	public List<Map> selectDetailListByOrderCode(String orderCode, String detailTable) {
		// TODO Auto-generated method stub
		return payMallInfoMapper.selectDetailListByOrderCode(orderCode, detailTable);
	}
	 /**
     * 系统自动下架
     * @return
     */
    public int cronJobsUpdateEnable(){
    	//商品库主表分组信息，返回表名及类型主键
    	List tableList = payMallInfoMapper.groupMainTable();
    	int num = 0;
    	if(tableList != null && tableList.size()>0)
    	{    		
    		//遍历所有主表，查询过期数据
    		for(int i=0;i<tableList.size();i++)
    		{
    			Map map = (Map)tableList.get(i);
    			String maint = String.valueOf(map.get("main_table"));//主表表名
    			String typeid = String.valueOf(map.get("id"));//主表表名
    			String ids = payMallInfoMapper.selectMainListByEndTime(maint);//获得所有过期的主键
    			//更新商品库中mainId in ids且type=本类的数据 “下架”
    			num += payMallInfoMapper.updateEnable(Long.parseLong(typeid), ids);
    		}
    		return num;
    	}
    	return num;
    }
}